<?php
	/**
	 * 商品内容控制器
	 */
	class GoodscontAction extends Action{
		
		//商品内容页视图
		public function index(){
			//当前所在位置使用等级栏目
			if($this->_get('gid')){
				$gid = $this->_get('gid' ,'intval');
				$db = D('GoodsCont');
				$goodsInfo = $db->getGoodsInfo($gid);
				//分配商品详细数据到视图
				$this->goodsInfo = $goodsInfo;
				// p($goodsInfo);
				$goodsCid = $goodsInfo['cid'];
				//分配当前位置数据到视图
				$parentCates = recursion(M('category')->select(),$goodsCid,2);
				$this->parentCates = $parentCates;
			}
			$this->display();
		}

		//相应异步请求要提取商品规格数据
		public function ajaxGetSpecData(){
			if($this->isPost()){
				//异步提取对应规格商品是否存在
				$gid = $this->_post('gid' ,'intval');
				$specId = rtrim($this->_post('specid') , ',');
				$specIds = explode(',', $specId);
				
				$db = M('goods_list');
				if(count($specIds)==2){
					$fattr1 = implode('|', array($specIds[0],$specIds[1]));
					$fattr2 = implode('|', array($specIds[1],$specIds[0]));
					$where = 'attr="' . $fattr1 . '" or ' . 'attr="' . $fattr2 . '"';
					$inventory = $db->where($where)->field('inventory')->find();
					if(is_array($inventory)){
						$getPrice = D('GoodsPrice');
						$where = 'goods.id='.$gid.' and (goods_attr.id='.$specIds[0].' or ' . 'goods_attr.id='.$specIds[1].')';
						$gsPrice = $getPrice->where($where)->select();
						$totalPrice = 0;
						foreach($gsPrice as $val){
							$totalPrice += $val['price'];
						}
						$totalPrice += $gsPrice[0]['sprice'];
						$inventory['totalPrice'] = $totalPrice;
					}
				}

				//取其中一个规格的id进行检索是否有相应配对规格的商品
				$specId = $specIds[0];
				$result = $db->where(array('gid'=>$gid))->field('inventory,attr')->select();
				foreach($result as $key=>$val){
					$result[$key]['attr'] = explode('|', $val['attr']);
				}
				$array = array();
				foreach($result as $key => $val){
					if(in_array($specId, $val['attr'])){
						foreach($val['attr'] as $v){
							if((int)$v != $specId){
								array_push($array, $v);		
							}
						}
					}
				}
				if(is_array($inventory)){
					array_push($array, 'i'.$inventory['inventory'].'|'.number_format($inventory['totalPrice'],2));
				}
				echo json_encode($array);
			}
		}
	}
?>